<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>查找一篇文章中出现频率最高的单词</title>
</head>
<!-- https://www.cnblogs.com/byronic/p/4539416.html -->

<body>
  <!-- 
function counts(article){
    article = article.trim().toUpperCase();
    var array = article.match(/[A-z]+/g);
    article = " "+array.join("  ")+" ";
    var max = 0,word,num = 0,maxword="";
    for(var i = 0; i < array.length; i++) {        
        word = new RegExp(" "+array[i]+" ",'g');
    num = article.match(word).length;
    if(num>max){
        max=num;
        maxword = array[i];
    }
   }
   console.log(maxword+" "+max);
}
counts("Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day;");
 -->
</body>
<script>
  "use strict"; {
    const counts = article => {
      article = article.trim().toUpperCase(); //去除头和尾并转为大写
      let array = article.match(/[A-z]+/g);
      article = ` ${array.join(" ")} `;
      let max = 0, word, num = 0, maxword = "";
      for (let i = 0; i < array.length; i++) {
        word = new RegExp(" " + array[i] + " ", 'g');
        num = article.match(word).length;
        if (num > max) {
          max = num;
          maxword = array[i];
        };
      };
      console.log(maxword + " " + max);
    };
    counts("Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day;")
  };
</script>

</html>